﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ex5_1
{
    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
        }
        Boolean printError(TextBox name, TextBox pwd)
        {
            if (name.TextLength == 0 || pwd.TextLength == 0)
            {
                MessageBox.Show("用户名或密码不能为空");
                return true;
            }
            else
            {
                return false;
            }
        }
        void connectSqlRegister()
        {
            SqlConnection conn = new SqlConnection("Data Source=192.168.30.1;Initial Catalog=STU;Persist Security Info=True;User ID=tt;Password=123456");
            conn.Open();


            SqlCommand cmd = new SqlCommand("set nocount off;insert into stuinfo values('" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')", conn);


            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            int n;
            try
            {
                n = cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {

                n = 0;
            }

            if (n != 0)
            {
                MessageBox.Show("注册成功");
            }
            else
            {
                MessageBox.Show("注册失败，用户名已存在");
            }
            conn.Close();
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

           
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }
       

        private void button3_Click(object sender, EventArgs e)
        {
            if (!printError(textBox3, textBox4))
            {
                connectSqlRegister();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}
